package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.dayconfig;

import cn.iocoder.yudao.module.fengdong.smartelderly.utils.JsonUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder.getTenantId;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.dayconfig.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.dayconfig.DayconfigDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.dayconfig.DayconfigConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.dayconfig.DayconfigService;

@Tag(name = "管理后台 - 月缴费配置")
@RestController
@RequestMapping("/fengdong.smartelderly/dayconfig")
@Validated
public class DayconfigController {

    @Resource
    private DayconfigService dayconfigService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/create")
    @Operation(summary = "创建月缴费配置")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:create')")
    public CommonResult<Long> createDayconfig(@Valid @RequestBody DayconfigCreateReqVO createReqVO) {
        return success(dayconfigService.createDayconfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新月缴费配置")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:update')")
    public CommonResult<Boolean> updateDayconfig(@Valid @RequestBody DayconfigUpdateReqVO updateReqVO) {
        dayconfigService.updateDayconfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除月缴费配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:delete')")
    public CommonResult<Boolean> deleteDayconfig(@RequestParam("id") Long id) {
        dayconfigService.deleteDayconfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得月缴费配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:query')")
    public CommonResult<DayconfigRespVO> getDayconfig(@RequestParam("id") Long id) {
        DayconfigDO dayconfig = dayconfigService.getDayconfig(id);
        return success(DayconfigConvert.INSTANCE.convert(dayconfig));
    }

    @GetMapping("/executeConfig")
    @Operation(summary = "执行配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:execute')")
    public CommonResult<DayconfigRespVO> getExecuteConfig(@RequestParam("id") Long id) {
        DayconfigDO dayconfig = dayconfigService.getDayconfig(id);

        // 创建 MessageProperties 对象并设置头信息
        MessageConverter converter = rabbitTemplate.getMessageConverter();

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("tenant-id", getTenantId().toString());
        messageProperties.setHeader("execute", "true");
        Message message = converter.toMessage(JsonUtils.objToJson(dayconfig), messageProperties);
        rabbitTemplate.convertAndSend("myDayconfigMqExchange", "DayconfigMqKey", message);

        return success(DayconfigConvert.INSTANCE.convert(dayconfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得月缴费配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:query')")
    public CommonResult<List<DayconfigRespVO>> getDayconfigList(@RequestParam("ids") Collection<Long> ids) {
        List<DayconfigDO> list = dayconfigService.getDayconfigList(ids);
        return success(DayconfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得月缴费配置分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:query')")
    public CommonResult<PageResult<DayconfigRespVO>> getDayconfigPage(@Valid DayconfigPageReqVO pageVO) {
        PageResult<DayconfigDO> pageResult = dayconfigService.getDayconfigPage(pageVO);
        return success(DayconfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出月缴费配置 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:dayconfig:export')")
    @OperateLog(type = EXPORT)
    public void exportDayconfigExcel(@Valid DayconfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DayconfigDO> list = dayconfigService.getDayconfigList(exportReqVO);
        // 导出 Excel
        List<DayconfigExcelVO> datas = DayconfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "月缴费配置.xls", "数据", DayconfigExcelVO.class, datas);
    }

}
